using UnityEngine;

public class TankController : MonoBehaviour
{
    [Header("坦克属性")]
    public float moveSpeed = 5f;
    public float rotateSpeed = 100f;
    public float fireRate = 0.5f;
    
    [Header("引用组件")]
    public GameObject bulletPrefab;
    public Transform firePoint;
    
    private float nextFireTime;
    private VirtualJoystick moveJoystick;
    private VirtualJoystick rotateJoystick;
    
    void Start()
    {
        // 获取场景中的虚拟摇杆引用
        moveJoystick = GameObject.Find("MoveJoystick").GetComponent<VirtualJoystick>();
        rotateJoystick = GameObject.Find("RotateJoystick").GetComponent<VirtualJoystick>();
    }
    
    void Update()
    {
        // 移动控制
        Vector2 moveInput = moveJoystick.GetInputDirection();
        transform.Translate(new Vector3(moveInput.x, 0, moveInput.y) * moveSpeed * Time.deltaTime);
        
        // 旋转控制
        Vector2 rotateInput = rotateJoystick.GetInputDirection();
        if (rotateInput.magnitude > 0.1f)
        {
            float angle = Mathf.Atan2(rotateInput.x, rotateInput.y) * Mathf.Rad2Deg;
            transform.rotation = Quaternion.Lerp(transform.rotation, 
                Quaternion.Euler(0, angle, 0), 
                rotateSpeed * Time.deltaTime);
        }
        
        // 开火控制
        if (Input.GetButtonDown("Fire1") && Time.time >= nextFireTime)
        {
            Fire();
        }
    }
    
    void Fire()
    {
        nextFireTime = Time.time + fireRate;
        Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    }
} 